<?php

namespace App\Models;
use Carbon\Carbon;
use Framework\BaseModel;

/**
 * App\Models\_EmptyModel
 *
 * @method static \Illuminate\Database\Eloquent\Builder|_EmptyModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|_EmptyModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|_EmptyModel query()
 * @mixin \Eloquent
 */
class CronModel extends BaseModel
{
    protected $table = 'crons';

    protected $fillable = [
        'type', 'period', 'period_type', 'name',
        'event', 'execute_time','relate_id','is_free'
    ];


    /**
     *
     * 添加定时任务
     * @param int $type
     * @param int $period
     * @param $name
     * @param $event
     * @param $execute_time
     * @param $relate_id
     * @param int $period_type
     * @return bool
     */
    public static function addCron($type, $period, $name, $event, $execute_time, $relate_id, $period_type = 0)
    {
        $data = [
            'type'         => $type,
            'period'       => $period,
            'period_type'  => $period_type,
            'name'         => $name,
            'event'        => $event,
            'execute_time' => $execute_time,
            'relate_id'    => $relate_id,
            'created_at'   =>Carbon::now(),
            'updated_at'   =>Carbon::now(),
        ];
        $cron =self::where(['event'=>$event,'relate_id'=>$relate_id]);
        if ($cron) {
            $cron->delete();
        }
        $res  = self::query()->insertGetId($data);
        return $res;
    }





    /**
     *  删除计划任务
     * @param $condition
     * @return mixed
     */
    public static function deleteCron($condition)
    {
        $res = self::query()->where($condition)->delete();
        return $res;
    }
}
